package com.photoeditor.demo.util;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.demo.ui.widget.gallery.GalleryRowItem;
import com.photoeditor.demo.util.gallery.util.RectEvaluator;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.model.bean.ThumbnailBean;


public class ShowBigImageUtils {
    private FrameLayout mBigLayout;
//    private TileImageView mBigImage;
    private ImageView mBigImage;
    private ObjectAnimator animator;
//    private DecoderLoader mDecoderLoader = new DecoderLoader();
    private RectEvaluator sRectEvaluator = new RectEvaluator(new Rect());
    private ArgbEvaluator mColorEvaluator = new ArgbEvaluator();
    private static final int START_COLOR = Color.TRANSPARENT;
    private static final int END_COLOR = Color.parseColor("#b2000000");
    public static final int ANIMATION_TIME = 150;

    private ImageLoader.OnLoadBitmapListener mListener;

    public void showImageView(final Activity mActivity, final ViewGroup parentView, final GalleryRowItem item,
                              final ThumbnailBean bean, final RectF startRectF, @NonNull final Bitmap bitmap, Bitmap bgBitmap, boolean vibrator){
        if(vibrator) {
            startVibrator(mActivity, 25);
        }
        mListener = new ImageLoader.OnLoadBitmapListener() {
            @Override
            public void onLoadBitmapFinish(Object bitmap) {
                if (mBigImage != null && bitmap != null && bitmap instanceof Bitmap) {
                    mBigImage.setBackgroundColor(Color.TRANSPARENT);
                    mBigImage.setImageBitmap((Bitmap) bitmap);
                }
            }
        };
        dealAnimation(mActivity, parentView, item, bean, startRectF, bitmap);
        mBigImage.setImageBitmap(null);
        if (bean.getUri() != null) {
            ImageLoader.loadImageOnly(mActivity, bean.getUri().toString(), mListener);
        }
    }

    private void dealAnimation(Activity mActivity, ViewGroup parentView, GalleryRowItem item, ThumbnailBean bean, RectF startRectF, Bitmap bitmap){
        ViewGroup decorView;
        boolean isTips = false;
        if(parentView == null){
            decorView = (ViewGroup)mActivity.getWindow().getDecorView();
        } else{
            isTips = true;
            decorView = parentView;
        }
        final int decorWidth = decorView.getWidth();
        final int decorHeight = decorView.getHeight();
        final int left = DeviceUtils.dip2px(mActivity, 10);
        final int top = DeviceUtils.dip2px(mActivity, 56);
        if(mBigImage == null) {
            mBigLayout = new FrameLayout(mActivity);
            ViewGroup.LayoutParams layoutLp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT);

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(decorWidth - left * 2 , decorHeight - top * 2);
            lp.gravity = Gravity.CENTER;
            mBigImage = new ImageView(mActivity);
            mBigLayout.addView(mBigImage, lp);
            if(isTips){
                decorView.addView(mBigLayout, decorView.getChildCount() - 1, layoutLp);//增加到倒数第二位
            } else{
                decorView.addView(mBigLayout, layoutLp);//增加到最后一位
            }
            animator = ObjectAnimator.ofObject(mBigImage, "clipBounds", sRectEvaluator, new Rect());
            animator.setDuration(ANIMATION_TIME);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float fraction = animation.getAnimatedFraction();
                    int color = (int)mColorEvaluator.evaluate(fraction, START_COLOR, END_COLOR);
                    mBigLayout.setBackgroundColor(color);
                }
            });
        }
//        mBigLayout.setBackgroundDrawable(new BitmapDrawable(bgBitmap));
        mBigLayout.setBackgroundColor(START_COLOR);

        RectF viewRectF = ImageRectUtils.getViewRect(item);
        startRectF.offset(viewRectF.left, viewRectF.top);

        //最大的Rect
        final Rect endRect = new Rect(left, top, decorWidth - left, decorHeight - top);

        int bw = bitmap.getWidth();
        int bh = bitmap.getHeight();
        int endW = endRect.width();
        int endH = endRect.height();

        int newW, newH;
        if(bw * 1.0f / bh > endW * 1.0f / endH){//比较宽  宽最大
            newW = endW;
            newH = (int)(newW * 1.0f * bh / bw + 0.5f);

            int dertHeight = (endH - newH) / 2;
            endRect.set(endRect.left, endRect.top + dertHeight, endRect.right, endRect.bottom - dertHeight);
        } else{//高最大
            newH = endH;
            newW = (int)(newH * 1.0f * bw / bh + 0.5f);

            int dertWeight = (endW - newW) / 2;
            endRect.set(endRect.left + dertWeight, endRect.top, endRect.right - dertWeight, endRect.bottom);
        }
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)mBigImage.getLayoutParams();
        lp.width = endRect.width();
        lp.height = endRect.height();
        mBigImage.setLayoutParams(lp);
        mBigImage.setBackgroundDrawable(new BitmapDrawable(bitmap));

        Rect startRect = new Rect((int)startRectF.left, (int)startRectF.top, (int)startRectF.right, (int)startRectF.bottom);
        startRect.offset(-endRect.left, -endRect.top);
        endRect.offset(-endRect.left, -endRect.top);

        boolean intersect = Rect.intersects(endRect, startRect);
        if(!intersect){//完全不相交的情况下某些手机Clip不管用  所以需要特殊处理
            int startCenterX = startRect.centerX();
            int startCenterY = startRect.centerY();
            int endCenterX = endRect.centerX();
            int endCenterY = endRect.centerY();

            if(startCenterX > endCenterX){
                if(startCenterY > endCenterY){//右下角
                    startRect.set(endRect.right - 1, endRect.bottom - 1, endRect.right, endRect.bottom);
                } else{//右上角
                    startRect.set(endRect.right - 1, endRect.top, endRect.right, endRect.top + 1);
                }
            } else{
                if(startCenterY > endCenterY){//左下角
                    startRect.set(endRect.left, endRect.bottom - 1, endRect.left + 1, endRect.bottom);
                } else{//左上角
                    startRect.set(endRect.left, endRect.top, endRect.left + 1, endRect.top + 1);
                }
            }
        }

        animator.setObjectValues(startRect, endRect);
        animator.start();
        mBigImage.setVisibility(View.VISIBLE);
    }

    public void hideImageView(){
        if(mBigImage == null) return;
        animator.end();
        mBigLayout.setBackgroundColor(START_COLOR);
        mBigImage.setVisibility(View.GONE);
        ImageLoader.removeListener(mListener);
        mListener = null;
    }

    public void startVibrator(Context context, int time){
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(time);
    }
}
